package cn.edu.xmu.oomall.customer.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.util.JwtHelper;
import cn.edu.xmu.javaee.core.validation.NewGroup;
import cn.edu.xmu.javaee.core.validation.UpdateGroup;
import cn.edu.xmu.oomall.customer.controller.dto.CustomerDto;
import cn.edu.xmu.oomall.customer.controller.vo.CustomerVo;
import cn.edu.xmu.oomall.customer.dao.bo.Customer;
import cn.edu.xmu.oomall.customer.service.CustomerService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.edu.xmu.javaee.core.util.CloneFactory;

/**
 * @author 谢传艳
 * @date 2024/12/18
 */
@RestController
@RequestMapping(value = "/customers", produces = "application/json;charset=UTF-8")
@RequiredArgsConstructor
@Transactional
public class CustomerController {
    private final CustomerService customerService;

    /**
     * 用户注册
     * @param customerDto 用户信息
     */
    @PostMapping
    public ReturnObject registerUser(@Validated(NewGroup.class) @RequestBody CustomerDto customerDto) {
        Customer customer = CloneFactory.copy(new Customer(), customerDto);
        customer = customerService.registerUser(customer);
        return new ReturnObject(ReturnNo.CREATED, CloneFactory.copy(new CustomerVo(customer), customer));
    }

    /**
     * 获取自己的信息
     * @param user 当前登录用户
     */
    @Audit(departName = "customers")
    @GetMapping
    public ReturnObject getSelfInfo(@LoginUser UserDto user){
        Customer customer = customerService.getSelfInfo(user.getId());
        return new ReturnObject(ReturnNo.OK, CloneFactory.copy(new CustomerVo(customer), customer));
    }

    /**
     * 修改自己的信息
     * @param user 当前登录用户
     * @param customerDto 用户信息
     */
    @Audit(departName = "customers")
    @PutMapping("")
    public ReturnObject modifiedMyselfInfo(@LoginUser UserDto user,
                                           @Validated(UpdateGroup.class) @RequestBody CustomerDto customerDto){
        Customer customer = CloneFactory.copy(new Customer(), customerDto);
        customer.setId(user.getId());
        customerService.modifiedMyselfInfo(customer, user);
        return new ReturnObject(ReturnNo.OK);
    }

    /**
     * 用户登录
     * @param customerDto 用户信息
     * @param request 请求
     */
    @PostMapping("/login")
    public ReturnObject login(@RequestBody CustomerDto customerDto,
                              HttpServletRequest request){
        Customer customer = CloneFactory.copy(new Customer(), customerDto);
        String loginToken = customerService.login(customer);
        request.setAttribute(JwtHelper.LOGIN_TOKEN_KEY, loginToken);
        return new ReturnObject(ReturnNo.OK, loginToken);
    }

    /**
     * 退出登录
     * @param user 当前登录用户
     * @param request 请求
     */
    @Audit(departName = "customers")
    @GetMapping("/{id}/logout")
    public ReturnObject logout(@LoginUser UserDto user,
                               HttpServletRequest request){
        request.removeAttribute(JwtHelper.LOGIN_TOKEN_KEY);
        return new ReturnObject(ReturnNo.OK);
    }

}
